#ifndef __MY_UTIL__
#define __MY_UTIL__
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>

namespace aod
{
    class FileUtil
    {
    private:
        std::string _name;

    public:
        FileUtil(const std::string name) : _name(name){};
        // 判断文件是否存在
        bool Exists()
        {
            // F_OK用于检测文件是否存在，存在则返回0
            int ret = access(_name.c_str(), F_OK);
            if (ret != 0)
                return false;
            else
                return true;
        }
        // 获得文件大小
        size_t Size()
        {
            if (this->Exists() == false)
                return 0;
            struct stat st;
            // stat接口用于获取文件的属性，其中st_size是文件大小成员
            int ret = stat(_name.c_str(), &st);
            if (ret != 0)
                return 0;

            return st.st_size;
        }
        // 读取文件数据到body
        bool GetContent(std::string *body) // 输出型参数
        {
            std::ifstream ifs;
            ifs.open(_name, std::ios::binary);
            if (ifs.is_open() == false)
            {
                std::cout << "GetContent open file failed";
                return false;
            }
            size_t flen = this->Size();
            body->resize(flen);
            ifs.read(&(*body)[0], flen);
            if (ifs.good() == false)
            {
                std::cout << "read file content failed" << std::endl;
                return false;
            }
            ifs.close();
            return true;
        }
        // 向文件中写入数据
        bool SetContent(const std::string &body)
        {
            std::ofstream ofs;
            ofs.open(_name, std::ios::binary);
            if (ofs.is_open() == false)
            {
                std::cout << "SetContent open file failed";
                return false;
            }
            ofs.write(body.c_str(), body.size());
            if (ofs.good() == false)
            {
                std::cout << "write file content failed " << std::endl;
                return false;
            }
            ofs.close();
            return true;
        }
        // 针对目录时创建目录
        bool CreateDirectory()
        {
            //??
            if (this->Exists())
            {
                return true;
            }
            mkdir(_name.c_str(), 0777);
            return true;
        }
    };

    class JsonUtil{
    public:
    static bool Serialize (const Json::Value & value ,std::string * body)
    {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(value,&ss);
        if(ret != 0)
        {
            std::cout << "Serialize failed"<<std::endl;
            return false;
        }
        *body = ss.str();
        return true;
    }
    static bool UnSerialize (const std::string &body ,Json ::Value * value)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        bool ret =cr->parse(body.c_str() , body.c_str()+body.size(), value, &err);
        if(ret == false)
        {
            std::cout <<"UnSerialize failed"<<std::endl;
            return false;
        }
        return true;
    }
    };

}


#endif